package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.ProcessApproveDTO;
import com.neusoft.neuiotms.dcam.workflow.model.ProcessApprove;
import com.neusoft.neuiotms.dcam.workflow.repository.ProcessApproveRepository;

@Component
public class ProcessApproveConverter extends AbstractConverter<ProcessApprove, ProcessApproveDTO> {
    @Autowired
    private ProcessApproveRepository processApproveRepository;

    @Override
    public ProcessApprove toModel(final ProcessApproveDTO dto) {
        ProcessApprove model;
        if (dto.isNew()) {
            model = new ProcessApprove();
        } else {
            model = this.processApproveRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setUpdateTime(new Date());
        model.setOpinion(dto.getOpinion());
        model.setReason(dto.getReason());

        return model;
    }

    @Override
    public ProcessApproveDTO toDTO(final ProcessApprove model, final boolean forListView) {
        final ProcessApproveDTO dto = new ProcessApproveDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setOpinion(model.getOpinion());
        dto.setReason(model.getReason());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

}
